/*
 * CatPred.java
 *
 * Created on den 2 maj 2007, 09:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package grex.Nodes;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import grex.Data.PredictionContainer;
import grex.*;
import grex.Data.Prediction;
import java.text.NumberFormat;

public class Val extends Node implements IOperatorTerminal{

    private Var var;
    private static final int NROFCILDREN = 0;
    private double geneNumericalValue = -1;
    private double geneValue = -1;
    private String realValue = "Not initialized";
    

    public Val() {
        super(NROFCILDREN);
        geneValue = Options.rnd.nextDouble();
    }


    public synchronized void execute(Node parent, PredictionContainer data, int mode) throws GeneException {
        this.parent = parent;
        if (mode == Node.TRAIN_MODE) {
            geneNumericalValue = getGeneNumericalValue();
        }

        for (Prediction p : data.values()) {
            p.setPrediction(geneNumericalValue);
        }
    }
    public double getGeneNumericalValue(){            
            var = getConnectedVar();
            return environment.getInternalNumericalValue(var.getVarNr(), geneValue,getFold());
    }
    private Var getConnectedVar(){
        Node[] childs = parent.getChildren();
        var = (Var) (childs[0]==this?childs[1]:childs[0]);
        return var;
    }

    public double getInternalValue() {

        return geneNumericalValue;
    }
    public void setInternalValue(double value){
        geneValue = value;
        this.geneNumericalValue = value;
    }
    public void setValValue(double val){
        this.geneValue= val;
      //  if(parent != null)
       //     geneNumericalValue = getGeneNumericalValue();
    }
    public double getValValue(){
        return geneValue;
    }
    
    public void setRealValue(String realValue){
        this.realValue=realValue;
    }

    public Object clone() {
        Val val = new Val();
        val.setFold(getFold());
        val.geneNumericalValue= geneNumericalValue;
        val.geneValue=geneValue;
//        val.var=var;
        val.setOptions(ops);
        val.setEnvironment(environment);
        val.setRealValue(realValue);
//        val.setParent(parent);
        return val;
    }
     public boolean equals(Object o){
        if(o instanceof Val){
            Val v =(Val) o;
            if(geneNumericalValue==v.getInternalValue())
               return true;
        }
        return false;
    }
    
    public String toString() {
       if(parent!=null)      {      
           
          

           return getConnectedVar().getGeneRealValue(geneNumericalValue);
              
       }
        /*NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(3);
        if(parent!=null){
            //if(var==null)
            geneNumericalValue = getGeneNumericalValue();
            geneNumericalValue = environment.getInternalNumericalValue(var.getVarNr(), geneValue,getFold());
            if(parent instanceof ICategoricalOperator)
                realValue = environment.getRealClassValue(var.getVarNr(), geneNumericalValue);//""+internalValue;//
            else
                realValue = nf.format(geneNumericalValue);
        }*/
        return realValue;
    }




    public double execute(Node parent, Prediction prediction)throws GeneException {
        this.parent = parent;
        geneNumericalValue = getGeneNumericalValue();
        return geneNumericalValue;

    }
}

